package com.yc.demo.servlet.login;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * 登录场景描述：
 * 1. 打开登录页
 * 2. 发起登录请求
 * 3. 到其他Servlet ，验证会话中登录数据
 * 
 */
@WebServlet("/login.s")
public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    	
    	String name = request.getParameter("name");
    	String pwd = request.getParameter("pwd");
    	response.setContentType("text/html;charset=utf-8");
    	if("yc".equals(name) && "123".equals(pwd)) {
    		/**
    		 *  如果登录成功， 那么在会话对象中保存一个值
    		 */
    		// 获取会话对象
    		HttpSession session = request.getSession();
    		// 向会话中保存一个数据（登录状态）
    		session.setAttribute("loginedUser", name);
    		response.getWriter().append("登录成功！");
    	} else {
    		// TODO 
    	}
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
